/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Hua YY
 * Date: 2024-12-24
 * Time: 19:58
 */
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution1 {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        //先new一个虚拟节点
        ListNode newHead = new ListNode(0);
        ListNode cur1 = l1 , cur2 = l2, cur3 = newHead;
        int t = 0;
        while(cur1 != null || cur2 != null || t != 0){
            //对第一个链表做加法
            if(cur1 != null){
                t += cur1.val;
                cur1 = cur1.next;
            }
            //对第二个链表做加法
            if(cur2 != null){
                t += cur2.val;
                cur2 = cur2.next;
            }
            //移动
            cur3.next = new ListNode(t%10);
            cur3 = cur3.next;
            t = t/10;
        }
        return newHead.next;
    }
}
public class Test1 {
}
